<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请输入">
                  <a-select-option value="1">使用中</a-select-option>
                  <a-select-option value="2">停用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新增收款方式</a-button>
        <a-button
          type="primary"
          @click="
            () => {
              this.$router.push({ path: `/system/paytype/payclassify` })
            }
          "
        >分类管理</a-button>
        <a-button 
          type="primary"  
          @click="
            () => {
              this.$router.push({ path: `/system/paytype/shoppay` })
            }
          ">门店收款管理</a-button>
        <!-- <a-button icon="cloud-upload" @click="handleAdd">导出</a-button> -->
      </div>

      <a-table 
        ref="table" 
        size="default" 
        :loading="loading"
        rowKey="key"
        :columns="columns"
        :data-source="loadData" 
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#f50" v-if="record?.status == 1">使用中</a-tag>
            <a-tag color="#2db7f5" v-if="record?.status == 2">停用</a-tag>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleSet(record)">收款门店设置</a>
          </template>
        </span>
      </a-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
// import moment from 'moment'
import { Ellipsis } from '@/components'
import { getPaymentMethodListUsingPOST, addPaymentMethodUsingPOST, updatePaymentMethodUsingPOST } from '@/api/api/xitongguanli.ts'
import CreateForm from './edit'
const columns = [
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '分类',
    dataIndex: 'paymentTypeName',
  },
  {
    title: '费率',
    dataIndex: 'rate',
  },
  {
    title: '封顶值',
    dataIndex: 'top',
  },
  {
    title: '到账账户',
    dataIndex: 'accountName',
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name: 'Paytype',
  components: {
    Ellipsis,
    CreateForm,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      loading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo:1,
        pageSize:10,
        queryCondition:'',
        status:''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
      pagination: {},
    }
  },
  created () {
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleSearch (params) {
      this.loading = false
      getPaymentMethodListUsingPOST(params).then((res) => {
        this.loadData = res.data.list
        this.loading = false
        this.queryParam.pageNo = this.pagination.current
        this.pagination = { ...this.pagination, total: Number(res.data.total) }
      })
      .catch((error) => {
        this.loading = false
        this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
      })
    },
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination)
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo:pagination.current,
        pageSize:pagination.pageSize,
        queryCondition:this.queryParam.queryCondition,
        status:this.queryParam.status,
        date:this.queryParam.date
      })
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (!!values.id) {
            // 修改 e.g.
            updatePaymentMethodUsingPOST({...values}).then(() => { 
              this.handleSearch(this.queryParam)
              this.$message.success('操作成功')
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
            })
            .catch((error) => {
              this.confirmLoading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          } else {
            // 新增
            addPaymentMethodUsingPOST({...values}).then(() => { 
              this.handleSearch(this.queryParam)
              this.$message.success('操作成功')
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
            })
            .catch((error) => {
              this.confirmLoading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSet(record) {
      this.$router.push({ path: `/system/paytype/shopset/${record.id}`,query:{name:record.name} })
    },
  },
}
</script>
